﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using NewtifyMe.DataTypes;
using NewtifyMe.Interfaces;

namespace Newtify.Heuristic
{
    /// <summary>
    /// 
    /// Concret implementation from Inferce IFeedParse
    /// to get feed from feed source and transform this to 
    /// local types
    /// 
    /// </summary>
    public class ParseFeeds : IFeedParse
    {
        public List<Feed> ParseAllFeeds(FeedSource source)
        {
            try
            {
                XDocument rssFeed = XDocument.Load( source.Url.Value );


                XDocument xDoc = XDocument.Load((source.Url.Value ));

                XNamespace atom = XNamespace.Get("http://www.w3.org/2005/Atom");
                XNamespace digg = XNamespace.Get("http://digg.com/docs/diggrss/");
                XNamespace media = XNamespace.Get("http://search.yahoo.com/mrss/");

                //String title = xDoc.Element(atom + "subtitle").Value;


                var items = xDoc
                            .Descendants( XNamespace.Get(source.FeedSourceSkeleton.Item.Namespace.Value) + source.FeedSourceSkeleton.Item.ValueHold.Value )
                            .Select(x => new Feed()
                            {
                                Title = x.Element(XNamespace.Get(source.FeedSourceSkeleton.ItemTitle.Namespace.Value) + source.FeedSourceSkeleton.ItemTitle.ValueHold.Value).Value,
                                //Link = x.Element(XNamespace.Get(source.FeedSourceSkeleton.ItemLink.Namespace.Value) + source.FeedSourceSkeleton.ItemLink.ValueHold.Value).Value,
                                UntagCategories = x.Elements(XNamespace.Get(source.FeedSourceSkeleton.ItemCategory.Namespace.Value) + source.FeedSourceSkeleton.ItemCategory.ValueHold.Value).ToList(),
                                Description = x.Element(XNamespace.Get(source.FeedSourceSkeleton.ItemDescription.Namespace.Value) + source.FeedSourceSkeleton.ItemDescription.ValueHold.Value).Value,
                                //PublisheDate = x.Element(XNamespace.Get(source.FeedSourceSkeleton.ItemPublicationDate.Namespace.Value) + source.FeedSourceSkeleton.ItemPublicationDate.ValueHold.Value).Value,
                                //Thumbnail = x.Element(media + "thumbnail").Attribute("url").Value
                            })
                            .ToList();


                String tmp = null;

                foreach (var feed in items)
                {
                    foreach (var VARIABLE in feed.UntagCategories)
                    {
                        tmp +="["+ VARIABLE.Value+ "]";
                    }
                    
                    feed.TESTE = tmp;
                    tmp = "";
                }

                return items;

            }
            catch (Exception ex)
            {
                
                throw ex;
            }

        }
    }
}
